﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace Prueba
{
    class Herramientas
    {

        public static void escribirKML(String campo, String lote, Double latitud, Double longitud, Double area, Double humedad, String fecha, String estado, String estilo, XmlTextWriter kml)
        {
            //escribe  los placemark en el archivo xml
            try
            {
                kml.WriteStartElement("Placemark");
                kml.WriteElementString("name", lote);
                String descripcion = "Campo: " + campo + "<br>Lote: " + lote + "<br>Area/Radio(mts): " + area + "<br>Humedad: " + humedad + "%" + "<br>Ultimo Control: " + fecha + "<br>Estado: " + estado;
                kml.WriteElementString("description", descripcion);
                Herramientas.estiloMarkers(kml, estilo);
                kml.WriteStartElement("Point");

                kml.WriteElementString("coordinates", "" + longitud + "," + latitud + "");

                kml.WriteEndElement(); // <Point>
                kml.WriteEndElement(); // <Placemark>
            }
            catch
            {
                MessageBox.Show("Error al intentar cargar los datos!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public static void estiloMarkers(XmlTextWriter kml, String estilo)
        {
            //escribo el estilo de los placemark
            try
            {
                if (estilo.Equals("peligro"))
                {
                    //peligro
                    kml.WriteStartElement("Style");
                    kml.WriteStartElement("IconStyle");
                    kml.WriteElementString("scale", "1.4");
                    kml.WriteStartElement("Icon");
                    kml.WriteElementString("href", "http://maps.google.com/mapfiles/kml/pushpin/red-pushpin.png");
                    kml.WriteEndElement();  // Icon
                    kml.WriteEndElement();  // IconStyle 
                    kml.WriteEndElement(); // Style

                }
                else if (estilo.Equals("cosechado"))
                {
                    //cosechado
                    kml.WriteStartElement("Style");
                    kml.WriteStartElement("IconStyle");
                    kml.WriteElementString("scale", "1.4");
                    kml.WriteStartElement("Icon");
                    kml.WriteElementString("href", "http://maps.google.com/mapfiles/kml/pushpin/blue-pushpin.png");
                    kml.WriteEndElement();  // Icon
                    kml.WriteEndElement();  // IconStyle 
                    kml.WriteEndElement(); // Style

                }
                else if (estilo.Equals("seguro"))
                {
                    //seguro
                    kml.WriteStartElement("Style");
                    kml.WriteStartElement("IconStyle");
                    kml.WriteElementString("scale", "1.4");
                    kml.WriteStartElement("Icon");
                    kml.WriteElementString("href", "http://maps.google.com/mapfiles/kml/pushpin/grn-pushpin.png");
                    kml.WriteEndElement();  // Icon
                    kml.WriteEndElement();  // IconStyle 
                    kml.WriteEndElement(); // Style

                }
                else if (estilo.Equals("intermedio"))
                {
                    //intermedio
                    kml.WriteStartElement("Style");
                    kml.WriteStartElement("IconStyle");
                    kml.WriteElementString("scale", "1.4");
                    kml.WriteStartElement("Icon");
                    kml.WriteElementString("href", "http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png");
                    kml.WriteEndElement();  // Icon
                    kml.WriteEndElement();  // IconStyle 
                    kml.WriteEndElement(); // Style

                }
                else if (estilo.Equals("sindato"))
                {
                    //sindato
                    kml.WriteStartElement("Style");
                    kml.WriteStartElement("IconStyle");
                    kml.WriteElementString("scale", "1.4");
                    kml.WriteStartElement("Icon");
                    kml.WriteElementString("href", "http://maps.google.com/mapfiles/kml/pushpin/wht-pushpin.png");
                    kml.WriteEndElement();  // Icon
                    kml.WriteEndElement();  // IconStyle 
                    kml.WriteEndElement(); // Style
                }
            }
            catch
            {
                MessageBox.Show("Error al intentar cargar los datos!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        public static List<String> leerArchivo(String directorio, String archivo)
        {
            List<String> listado = new List<string>();
            try
            {
                String line;
                StreamReader reader = new StreamReader(directorio + archivo);
                line = reader.ReadLine();
                while (line != null)
                {
                    listado.Add(line);
                    line = reader.ReadLine();
                }


                reader.Close();
                
            }
            catch
            {
                listado = null;
            
            }

            return listado;
        }

        public static void verificarDirectorio()
        {
            //comprueba la existencia cada directorio
            if (!Directory.Exists(Application.StartupPath + @"\HTML")){
				Directory.CreateDirectory(Application.StartupPath + @"\HTML");
        }
        
            if (!Directory.Exists(Application.StartupPath + @"\XML")) {
				Directory.CreateDirectory(Application.StartupPath + @"\XML");
        }
            if (!Directory.Exists(Application.StartupPath + @"\TXT")) {
				Directory.CreateDirectory(Application.StartupPath + @"\TXT");
        }
            if (!Directory.Exists(Application.StartupPath + @"\KML")) {
				Directory.CreateDirectory(Application.StartupPath + @"\KML");
        }
            if (!Directory.Exists(Application.StartupPath + @"\Datos Lotes")) {
				Directory.CreateDirectory(Application.StartupPath + @"\Datos Lotes");
        }
    }
    }
}

